using UnityEngine;
using System.Collections;

/// \class  gkSpawningInFrontOfTheScreen
/// \brief  Place the spawned actor in front of the screen
[AddComponentMenu("GK/Engine/Game/Spawn/gkSpawningInFrontOfTheScreen")]
public class gkSpawningInFrontOfTheScreen : gkSpawningModifier
{
    /// Scale the spawning volume width
    public float SpawningVolumeWidthScale = 1.0f;

    /// The game spawning bounding box
    private Bounds m_oSpawningVolumeBoudingBox;

    /// \brief  Called just after the game begin to play
    void Start()
    {
	   // Get the spawning volume bounding box
	   gkUtilityBounds.GrabColliderOrRendererBounds(GameObject.Find("GameVolume"), out m_oSpawningVolumeBoudingBox);
    }

    /// \brief  Event called at the and of the actor spawn
    public override void ModifySpawnedActor(GameObject a_rSpawnedActor, int a_iNumActorsInCurrentBatch, int a_iNbActorsInCurrentBatch)
    {
	    PlaceSpawnedActorInFrontOfTheScreen(a_rSpawnedActor);
    }

    /// \brief  Place the spawned actor just out of the screen
    void PlaceSpawnedActorInFrontOfTheScreen(GameObject a_rSpawnedActor)
    {
	    Vector3 f3SpawnedPosition;
		Bounds rSpawnedActorBounds;
		
		// Grab the spawned actor bounds
		 gkUtilityBounds.GrabRendererOrColliderBounds(a_rSpawnedActor, out rSpawnedActorBounds);
		
	    f3SpawnedPosition = a_rSpawnedActor.transform.position;

	    // Change the X coordinate in order to place the spawned actor just out of the screen
	    f3SpawnedPosition.x = m_oSpawningVolumeBoudingBox.max.x * SpawningVolumeWidthScale
		    + rSpawnedActorBounds.extents.x;

	    a_rSpawnedActor.transform.position = f3SpawnedPosition;
    }
}
